﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Prod;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageCooperation : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private ProdCooperationOrderEntity mSelectedCooperationOrder;
        #endregion

        #region ctors
        public frmManageCooperation()
        {
            InitializeComponent();
        }

        #endregion

        #region functions
        private void BindCombobox()
        {
            DataTable dtStatus = new DataTable();
            dtStatus.Columns.Add("name");
            dtStatus.Columns.Add("value", typeof(int));
            dtStatus.Rows.Add("", null);
            dtStatus.Rows.Add("新增", 0);
            dtStatus.Rows.Add("外协中", 1);
            dtStatus.Rows.Add("完成", 2);
            dtStatus.Rows.Add("已删除", 3);
            cmbStatus.DataSource = dtStatus;
            cmbStatus.DisplayMember = "name";
            cmbStatus.ValueMember = "value";

            DataTable dtTechnic = ProdTechnicBs.GetProdTechnicAll().Tables[0];
            repositoryItemGridLookUpEdit1.DataSource = dtTechnic;
            repositoryItemGridLookUpEdit1.DisplayMember = "TECHNIC_NAME";
            repositoryItemGridLookUpEdit1.ValueMember = "TECHNIC_CODE";

            repositoryItemGridLookUpEdit2.DataSource = dtStatus;
            repositoryItemGridLookUpEdit2.DisplayMember = "name";
            repositoryItemGridLookUpEdit2.ValueMember = "value";

            DataTable dtSupplier = MstSupplierBs.GetMstSupplierByCondition("", "", "");
            DataRow drSupplier = dtSupplier.NewRow();
            dtSupplier.Rows.Add(drSupplier);
            cmbSupplier.DisplayMember = "SUPPLIER_NAME";
            cmbSupplier.ValueMember = "SUPPLIER_ID";
            cmbSupplier.DataSource = dtSupplier;
        }

        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtCooperationOrder.Text) == false)
            {
                sCondition += " and pco.ORDER_CODE like '%" + txtCooperationOrder.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProdPlanOrder.Text) == false)
            {
                sCondition += " and ppo.PLAN_ORDER_CODE like '%" + txtProdPlanOrder.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            {
                sCondition += " and sppai.PRODUCT_SPEC_OUT like '%" + txtContractModel.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtWorkingOrder.Text) == false)
            {
                sCondition += " and ppoi.ORDER_ITEM_CODE like '%" + txtWorkingOrder.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                sCondition += " and mp.PRODUCT_CODE='" + txtProductCode.Text + "'";
            }
            if (dtpDeliveryDate.EditValue != null && string.IsNullOrEmpty(dtpDeliveryDate.EditValue.ToString()) == false)
            {
                sCondition += " and pco.PLAN_DELIVERY_DATE>='" + Convert.ToDateTime(dtpDeliveryDate.EditValue.ToString()).ToString("yyyy-MM-dd") + " 00:00:00'";
            }
            if (dtpDeliveryDateTo.EditValue != null && string.IsNullOrEmpty(dtpDeliveryDateTo.EditValue.ToString()) == false)
            {
                sCondition += " and pco.PLAN_DELIVERY_DATE<='" + Convert.ToDateTime(dtpDeliveryDateTo.EditValue.ToString()).ToString("yyyy-MM-dd") + " 23:59:59'";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                sCondition += " and pco.STATUS='" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                sCondition += " and pco.STATUS!=3";
            }
            if (cmbSupplier.SelectedValue != null)
            {
                if (string.IsNullOrEmpty(cmbSupplier.SelectedValue.ToString()) == false)
                {
                    sCondition += " and pco.SUPPLIER_ID='" + cmbSupplier.SelectedValue.ToString() + "'";
                }
            }
            return sCondition;
        }

        private void BindGrid()
        {
            string sCondition = GetCondition();
            DataTable dt = ProdCooperationOrderBs.GetDtAllByCondition(sCondition, txtRowCount.Text, "");
            DataColumn dtCol = new DataColumn();
            dtCol.ColumnName = "IS_SELECTED";
            dtCol.DataType = System.Type.GetType("System.Boolean");
            dtCol.DefaultValue = false;
            dt.Columns.Add(dtCol);
            gvMain.DataSource = dt;
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            BindGrid();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataTable dt = gvMain.DataSource as DataTable;
            if (e.RowHandle >= 0)
            {
                mSelectedCooperationOrder = new ProdCooperationOrderEntity(dt.Rows[e.RowHandle]);
                if ((bool)dt.Rows[e.RowHandle]["IS_SELECTED"])
                {
                    dt.Rows[e.RowHandle]["IS_SELECTED"] = false;
                }
                else
                {
                    dt.Rows[e.RowHandle]["IS_SELECTED"] = true;
                }
            }
        }

        protected override void  Click_View()
        {
            if (mSelectedCooperationOrder == null)
            {
                MessageBox.Show("请选中外协单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCooperation frm = new frmMaintainCooperation(mSelectedCooperationOrder);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainCooperation frm = new frmMaintainCooperation();
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedCooperationOrder == null)
            {
                MessageBox.Show("请选中外协单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCooperation frm = new frmMaintainCooperation(mSelectedCooperationOrder);
            frm.ThisActionType = ActionType.Modify;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (mSelectedCooperationOrder == null)
            {
                MessageBox.Show("请选中外协单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            mSelectedCooperationOrder.Status = 3;
            if (ProdCooperationOrderBs.UpdateProdCooperationOrder(mSelectedCooperationOrder))
            {
                MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                BindGrid();
                return;
            }
            else
            {
                MessageBox.Show("删除失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = gvMain.DataSource as DataTable;
            List<ProdCooperationOrderEntity> list = new List<ProdCooperationOrderEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"])
                {
                    ProdCooperationOrderEntity m = new ProdCooperationOrderEntity(dr);
                    list.Add(m);
                }
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请选中要打印的外协单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //frmPrintFormCooperationOrder frm = new frmPrintFormCooperationOrder(list);
            //if (frm.ShowDialog() == DialogResult.OK)
            //{
            //    BindGrid();
            //}
        }

        #endregion

        private void btnExport_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
        }
    }
}
